import 'package:eplorex/pages/message/group/group_create_page.dart';
import 'package:flutter/material.dart';

import '../../infra/websocket_utils.dart';
import 'contact/contact_page.dart';
import 'message_sub_page.dart';

class ConversationItem {
  final int cid;
  final String? lastOnlineTime;
  final String? convName;
  final int? senderId;
  final int? partnerId;
  final String? convUrl;
  String? lastMsg;
  int topRank;
  int? unreadCnt;

  ConversationItem(
      {required this.cid,
      required this.topRank,
      this.senderId,
      this.partnerId,
      this.lastOnlineTime,
      this.convName,
      this.lastMsg, this.unreadCnt,
      this.convUrl});

  factory ConversationItem.fromJson(Map map) {
    return ConversationItem(
        cid: map['cid'],
        senderId: map['senderId'],
        partnerId: map['partnerId'],
        convName: map['convName'],
        topRank: map['topRank'],
        convUrl: map['convUrl'],
        lastMsg: map['lastMsg'],
        lastOnlineTime: map['lastMsgTime']);
  }
}

class MessagePage extends StatefulWidget {
  Map title_sufix = {
    NetStatus.disconnected: '(未连接)',
    NetStatus.connecting: '(连接中...)',
    NetStatus.connected: '',
    NetStatus.reconnected: '',
  };

  @override
  State<MessagePage> createState() => _MessagePageState();
}

class _MessagePageState extends State<MessagePage>
    with TickerProviderStateMixin {
  var currentNetStatus = WebsocketUtils.netStatusNotifier.value;
  late TabController _tabController;
  late List<String> _tabs = ['消息', '联系人'];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: _tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: ValueListenableBuilder(
          valueListenable: WebsocketUtils.netStatusNotifier,
          builder: (BuildContext context, NetStatus value, Widget? child) {
            currentNetStatus = value;
            var titleSub = widget.title_sufix[currentNetStatus];
            return buildTabBar(titleSub);
          },
        ),
        centerTitle: true,
        actions: [
          Container(
            margin: EdgeInsets.only(right: 10),
            // width: 80,
            child: PopupMenuButton(
              padding: EdgeInsets.zero,
              icon: Icon(Icons.add_circle_outline),
              color: Colors.white,
              offset: const Offset(0, 50),
              itemBuilder: (BuildContext context) {
                return <PopupMenuEntry<String>>[
                  PopupMenuItem(
                    child: const Row(children: <Widget>[
                      Padding(
                          padding: EdgeInsets.fromLTRB(0.0, 0.0, 4.0, 0.0),
                          child: Icon(Icons.group_add)),
                      Text('创建新群')
                    ]),
                    onTap: () => Navigator.of(context).push(
                        MaterialPageRoute(builder: (_) => GroupCreatePage())),
                  ),
                  const PopupMenuItem(
                      child: Row(children: <Widget>[
                    Padding(
                        padding: EdgeInsets.fromLTRB(0.0, 0.0, 4.0, 0.0),
                        child: Icon(Icons.looks_one)),
                    Text('添加好友')
                  ])),
                  const PopupMenuItem(
                      child: Row(children: <Widget>[
                    Padding(
                        padding: EdgeInsets.fromLTRB(0.0, 0.0, 4.0, 0.0),
                        child: Icon(Icons.looks_two)),
                    Text('其他')
                  ])),
                ];
              },
            ),
          )
        ],
      ),
      body: TabBarView(controller: _tabController, children: [
        MessageSubPage(),
        ContactPage(onMessage: (_){

        },),
      ]),
    );
  }

  Widget buildTabBar(String titleSub) {
    return Container(
      child: TabBar(
        controller: _tabController,
        labelStyle: TextStyle(fontSize: 18),
        isScrollable: true,
        labelColor: Colors.black,
        tabAlignment: TabAlignment.center,
        labelPadding: EdgeInsets.fromLTRB(15, 0, 10, 5),
        indicator: const UnderlineTabIndicator(
          borderSide: BorderSide(color: Color(0xff1fcfbb), width: 3),
          insets: EdgeInsets.only(bottom: 10),
        ),
        tabs: [
          Tab(
            text: '消息$titleSub',
          ),
          Tab(
            text: '联系人',
          ),
        ],
      ),
    );
  }
}
